from baseview import BaseView
from ..bases import widgets
from .. import editors


class DeviceView(BaseView):
    topwidget_name = 'Devices'
    topwidget_cls = widgets.VPane
    def __init__(self, **kwargs):
        self.topwidget = widgets.HBox()
        super(DeviceView, self).__init__(**kwargs)
        self.DeviceSystem = self.MainController.DeviceSystem
        self.Devices = self.DeviceSystem.PatchedDevices
        self.current_device = None
        self.device_editor = None
        
        names = ['id', 'Num', 'Name', 'Universe', 'Start Chan', 'End Chan', 'Definition']
        self.device_list_attrs = ['Index', 'name', 'universe_id', 'start_chan', 'end_chan']
        types = [str, int, str, str, int, int, str]
        order = [1, 2, 3, 4, 5]
        self.device_list = widgets.TreeList(name='Devices', column_names=names, 
                                            list_types=types, column_order=order, 
                                            default_sort_column=1)
        self.update_device_list()
        self.device_list.connect('selection_changed', self.on_device_list_selection_changed)
        btn = widgets.Button(label='Delete Device')
        btn.connect('clicked', self.on_btnDelDevice_clicked)
        self.device_list.topwidget.pack_start(btn)
        self.topcontainer.pack_start(self.device_list.topwidget, expand=True)
        
        self.topwidget.show_all()
        
        self.Devices.bind(child_added=self.on_devices_child_added, 
                          child_removed=self.on_devices_child_removed)
    
    def unlink(self):
        self.Devices.unbind(self)
        for device in self.Devices.itervalues():
            device.unbind(self)
        super(DeviceView, self).unlink()
    
    def update_device_list(self, key=None):
        if key:
            devices = {key:self.Devices[key]}
        else:
            devices = self.Devices
        for key, val in devices.iteritems():
            univ = self.DeviceSystem.universes[val.universe_id]
            l = [getattr(val, attr) for attr in self.device_list_attrs]
            l[self.device_list_attrs.index('universe_id')] = univ.name
            l.append(val.definition.name)
            self.device_list.update({key:l})
            val.connect('property_changed', self.on_device_property_changed)
    
    def set_current_device(self, device):
        self.remove_editor('device_editor')
        if not device:
            return
        self.add_editor(editors.PatchedDeviceEditor, 'device_editor', device=device)
        self.topcontainer.pack_start(self.device_editor.topwidget)
        self.topcontainer.normalized_pos = .6
    
    def on_devices_child_added(self, **kwargs):
        obj = kwargs.get('obj')
        self.update_device_list(obj.id)
        
    def on_devices_child_removed(self, **kwargs):
        self.set_current_device(None)
        self.device_list.clear()
        self.update_device_list()
            
    def on_device_list_selection_changed(self, **kwargs):
        key = kwargs.get('key')
        device = self.Devices.get(key)
        if device:
            self.set_current_device(device)
        
    def on_btnDelDevice_clicked(self, btn):
        key = self.device_list.current_selection
        if key is None:
            return
        self.DeviceSystem.unpatch_device(id=key)
            
    def on_device_property_changed(self, **kwargs):
        obj = kwargs.get('obj')
        prop = kwargs.get('Property')
        if prop:
            name = prop.name
        else:
            name = kwargs.get('property')
        if name in self.device_list_attrs:
            self.update_device_list(obj.id)

UI_VIEW = DeviceView
